package cn.stylefeng.guns.modular.examination.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.examination.entity.Examination;
import cn.stylefeng.guns.modular.examination.mapper.ExaminationMapper;
import cn.stylefeng.guns.modular.examination.model.params.ExaminationParam;
import cn.stylefeng.guns.modular.examination.model.result.ExaminationResult;
import  cn.stylefeng.guns.modular.examination.service.ExaminationService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 创建考试 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-19
 */
@Service
public class ExaminationServiceImpl extends ServiceImpl<ExaminationMapper, Examination> implements ExaminationService {

    @Override
    public void add(ExaminationParam param){
        Examination entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ExaminationParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ExaminationParam param){
        Examination oldEntity = getOldEntity(param);
        Examination newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ExaminationResult findBySpec(ExaminationParam param){
        return null;
    }

    @Override
    public List<ExaminationResult> findListBySpec(ExaminationParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ExaminationParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ExaminationParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Examination getOldEntity(ExaminationParam param) {
        return this.getById(getKey(param));
    }

    private Examination getEntity(ExaminationParam param) {
        Examination entity = new Examination();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
